package com.hspedu.newData.dmsxl.贪心算法.mid;

import org.junit.Test;

import java.util.HashSet;
import java.util.Set;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-03 
 * @Version: 1.0
 */

public class LC1007行相等的最少多米诺旋转 {

    public int minDominoRotations(int[] tops, int[] bottoms) {

        int result = Integer.MAX_VALUE;
        // 要保证能翻转成功,top[0]和bottoms[0]一定包含了目标值
        // 以top[0]和bottoms[0]作为目标数，对tops和bottoms进行遍历统计即可得出结果
        Set<Integer> targetSet = new HashSet<>();
        targetSet.add(tops[0]);
        targetSet.add(bottoms[0]);
        for (int target : targetSet) {
            int topSum = 0;
            int botoomSum = 0;
            boolean flag = true;
            for (int j = 0; j < tops.length; j++) {
                if (tops[j] != target && bottoms[j] != target) {
                    // tops和bottoms都不包含目标值
                    flag = false;
                    break;
                } else {
                    if (tops[j] == target) {
                        topSum++;
                    }
                    if (bottoms[j] == target) {
                        botoomSum++;
                    }
                }
            }
            if (flag) {
                int item = tops.length - Math.max(topSum, botoomSum);
                result = Math.min(result, item);
            }
        }
        if (result == Integer.MAX_VALUE) {
            return -1;
        }
        return result;
    }

    @Test
    public void test() {
        // 测试代码

    }
}
